﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using App.Modelo;
using App.Modelo.Interfaces;
using App.SolInf.Controlador;
using System.Linq;

namespace App.SolInf.Vista
{

    public class GenericEditForm<T> : Form 
        where T :  new() 
    {
        public IRepositorio Session;
        public ModoFormularioABM Modo = ModoFormularioABM.Modificacion;

        private GroupBox groupBox1;
        private TableLayoutPanel tableLayoutPanel1;
        private TextBox textBox1;
        private BindingSource itemBindingSource;
        private System.ComponentModel.IContainer components;
        private Type Model;
        private ToolStrip toolStrip1;
        private ToolStripButton toolStripButton1;
        private ToolStripButton toolStripButton2;

        private T item;

        public T Item
        {
            get { return item; } 
            set { itemBindingSource.DataSource = item = value; }
        }

        public GenericEditForm()
        {
            InitializeComponent();

            Model = typeof(T);

            this.components = new System.ComponentModel.Container();

            this.itemBindingSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).BeginInit();


            var props = from p in Model.GetProperties()
                        where p.PropertyType.IsPublic == true
                        select p;

            Text = groupBox1.Text = Model.Name;

            this.tableLayoutPanel1.RowCount = props.Count();
            this.tableLayoutPanel1.Controls.Clear();


            this.tableLayoutPanel1.Height = props.Count()*30;

            var ix = 0;
            foreach (var field in props)
            {
                if (field.Name == "FechaBaja")
                    continue;

                this.tableLayoutPanel1.RowStyles.Add(
                    new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30));
                
                var label = new Label()
                                {
                                    AutoSize = true,
                                    Margin = new Padding(0),
                                    Text = field.Name
                                };
                var textBox = new TextBox()
                               {
                                   Margin = new Padding(3,0,3,0),
                                   Anchor =
                                       ((System.Windows.Forms.AnchorStyles)
                                        (((System.Windows.Forms.AnchorStyles.Top |
                                           System.Windows.Forms.AnchorStyles.Left)
                                          | System.Windows.Forms.AnchorStyles.Right))),


                               };

                if (field.Name == "Id")
                {
                    textBox.Enabled = false;
                }

                if (Modo == ModoFormularioABM.Ver)
                {
                    textBox.ReadOnly = true;
                }

                textBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemBindingSource, 
                    field.Name, true));

                this.tableLayoutPanel1.Controls.Add(label, 0, ix);
                this.tableLayoutPanel1.Controls.Add(textBox, 1, ix);
                ix++;
            }

            // 
            // itemBindingSource
            // 
            this.itemBindingSource.DataSource = Model;
            ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).EndInit();

        }

        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
            this.groupBox1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.tableLayoutPanel1);
            this.groupBox1.Location = new System.Drawing.Point(0, 3);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(392, 122);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "groupBox1";
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 120F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.textBox1, 1, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(3, 18);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(386, 60);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // textBox1
            // 
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(120, 0);
            this.textBox1.Margin = new System.Windows.Forms.Padding(0);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(266, 22);
            this.textBox1.TabIndex = 2;
            // 
            // toolStrip1
            // 
            this.toolStrip1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButton1,
            this.toolStripButton2});
            this.toolStrip1.Location = new System.Drawing.Point(0, 138);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
            this.toolStrip1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.toolStrip1.Size = new System.Drawing.Size(393, 43);
            this.toolStrip1.TabIndex = 3;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.AutoSize = false;
            this.toolStripButton1.Image = global::App.SolInf.Vista.Properties.Resources.bullet_disk;
            this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.Size = new System.Drawing.Size(100, 40);
            this.toolStripButton1.Text = "Guardar";
            this.toolStripButton1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.toolStripButton1.Click += new System.EventHandler(this._guardarCerrar);
            // 
            // toolStripButton2
            // 
            this.toolStripButton2.AutoSize = false;
            this.toolStripButton2.Image = global::App.SolInf.Vista.Properties.Resources.cancel;
            this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton2.Name = "toolStripButton2";
            this.toolStripButton2.Size = new System.Drawing.Size(100, 40);
            this.toolStripButton2.Text = "Cancelar";
            this.toolStripButton2.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.toolStripButton2.Click += new System.EventHandler(this._cerrar);
            // 
            // GenericEditForm
            // 
            this.ClientSize = new System.Drawing.Size(393, 181);
            this.Controls.Add(this.toolStrip1);
            this.Controls.Add(this.groupBox1);
            this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "GenericEditForm";
            this.groupBox1.ResumeLayout(false);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void _cerrar(object sender, EventArgs e)
        {
            Close() ;
        }

        private void _guardarCerrar(object sender, EventArgs e)
        {

            _guardar(sender, e);
            Close();
        }

        private void _guardar(object sender, EventArgs eventArgs)
        {
            Validate();
            try
            {
                switch (Modo)
                {
                    case ModoFormularioABM.Alta:
                        Session.SaveObject(Item);
                        Session.FlushSession();

                        MessageBox.Show("Se guardo el nuevo " + Model.Name, Text, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);

                        Modo = ModoFormularioABM.Modificacion;
                        
                        break;
                    case ModoFormularioABM.Modificacion:
                        Session.UpdateObject(Item);
                        Session.FlushSession();
                        MessageBox.Show("Se guardo el " + Model.Name, Text, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                        break;
                    case ModoFormularioABM.Baja:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public virtual void Alta()
        {
            Item = new T();
            Modo = ModoFormularioABM.Alta;
            ModuloSolInf.Instance.AbrirFormulario(this);
        }
        public virtual void Modificar()
        {
            Modo = ModoFormularioABM.Modificacion;
            ModuloSolInf.Instance.AbrirFormulario(this);
        }

        public virtual void Ver()
        {
            Modo = ModoFormularioABM.Ver;
            ModuloSolInf.Instance.AbrirFormulario(this);
        }
    }
}
